home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu248.dms / pu248.adf / Intuition / Menus / Example6.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  21KB  |  542 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Menus                       Tulevagen 22       */
  8. /* File:    Example6.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program opens a normal window to which we connect a menu strip.  */
  21. /* The menu consists of six small dices which are all action items. This */
  22. /* example shows how you can use Images inside a menu.                   */
  23.  
  24.  
  25.  
  26. #include <intuition/intuition.h>
  27.  
  28.  
  29.  
  30. struct IntuitionBase *IntuitionBase;
  31.  
  32.  
  33.  
  34. /*************************************************************************/
  35. /*                             D I C E   1                               */
  36. /*************************************************************************/
  37.  
  38. /* Data for dice 1: */
  39. USHORT chip dice1_data[]=
  40. {
  41.   0x0000,  /* 0000 0000 0000 0000   0 : Black  */
  42.   0x0000,  /* 0000 0000 0000 0000   1 : Orange */
  43.   0x0000,  /* 0000 0000 0000 0000              */
  44.   0x03C0,  /* 0000 0011 1100 0000              */
  45.   0x03C0,  /* 0000 0011 1100 0000              */
  46.   0x0000,  /* 0000 0000 0000 0000              */
  47.   0x0000,  /* 0000 0000 0000 0000              */
  48.   0x0000   /* 0000 0000 0000 0000              */
  49. };
  50.  
  51. /* Image structure for dice 1: */
  52. struct Image dice1=
  53. {
  54.   0,          /* LeftEdge, 0 pixels out. */
  55.   0,          /* TopEdge, 0 pixels down. */
  56.   16,         /* Width, 16 pixels wide. */
  57.   8,          /* Height, 8 lines heigh. */
  58.   1,          /* Depth, one Bitplane. */
  59.   dice1_data, /* ImageData, pointer to the image data. */
  60.   0x1,        /* PlanePick, affect Bitplane zero. */
  61.   0x2,        /* PlaneOnOff, fill Bitplane one with 1's. */
  62.   NULL        /* NextImage, no Image structure connected to this one. */
  63. };
  64.  
  65. /* The MenuItem structure for dice 1: */
  66. struct MenuItem my_dice1_item=
  67. {
  68.   NULL,            /* NextItem, this is the last item in the list. */
  69.   0,               /* LeftEdge, 0 pixels out. */
  70.   50,              /* TopEdge, 50 lines down. */
  71.   50,              /* Width, 50 pixels wide. */
  72.   8,               /* Height, 8 lines high. */
  73.   ITEMENABLED|     /* Flags, this item will be enabled. */
  74.                    /*        render this item with an Image. */
  75.                    /*        (ITEMTEXT is not set.) */
  76.                    /*        it is an action item. */
  77.                    /*        (CHECKIT is not set.) */
  78.   HIGHCOMP,        /*        complement the colours when highlihted. */
  79.   0x00000000,      /* MutualExclude, no mutualexclude. */
  80.   (APTR) &dice1,   /* ItemFill, pointer to the image. */
  81.   NULL,            /* SelectFill, nothing since we complement the col. */
  82.   0,               /* Command, no command-key sequence. */
  83.   NULL,            /* SubItem, no subitem list. */
  84.   MENUNULL,        /* NextSelect, no items selected. */
  85. };
  86.  
  87.  
  88.  
  89. /*************************************************************************/
  90. /*                             D I C E   2                               */
  91. /*************************************************************************/
  92.  
  93. /* Data for dice 2: */
  94. USHORT chip dice2_data[]=
  95. {
  96.   0x0000,  /* 0000 0000 0000 0000   0 : Black  */
  97.   0x7800,  /* 0111 1000 0000 0000   1 : Orange */
  98.   0x7800,  /* 0111 1000 0000 0000              */
  99.   0x0000,  /* 0000 0000 0000 0000              */
  100.   0x0000,  /* 0000 0000 0000 0000              */
  101.   0x001E,  /* 0000 0000 0001 1110              */
  102.   0x001E,  /* 0000 0000 0001 1110              */
  103.   0x0000   /* 0000 0000 0000 0000              */
  104. };
  105.  
  106. /* Image structure for dice 2: */
  107. struct Image dice2=
  108. {
  109.   0,          /* LeftEdge, 0 pixels out. */
  110.   0,          /* TopEdge, 0 pixels down. */
  111.   16,         /* Width, 16 pixels wide. */
  112.   8,          /* Height, 8 lines heigh. */
  113.   1,          /* Depth, one Bitplane. */
  114.   dice2_data, /* ImageData, pointer to the image data. */
  115.   0x1,        /* PlanePick, affect Bitplane zero. */
  116.   0x2,        /* PlaneOnOff, fill Bitplane one with 1's. */
  117.   NULL        /* NextImage, no Image structure connected to this one. */
  118. };
  119.  
  120. /* The MenuItem structure for dice 2: */
  121. struct MenuItem my_dice2_item=
  122. {
  123.   &my_dice1_item,  /* NextItem, pointer to the next item in the list. */
  124.   0,               /* LeftEdge, 0 pixels out. */
  125.   40,              /* TopEdge, 40 lines down. */
  126.   50,              /* Width, 50 pixels wide. */
  127.   8,               /* Height, 8 lines high. */
  128.   ITEMENABLED|     /* Flags, this item will be enabled. */
  129.                    /*        render this item with an Image. */
  130.                    /*        (ITEMTEXT is not set.) */
  131.                    /*        it is an action item. */
  132.                    /*        (CHECKIT is not set.) */
  133.   HIGHCOMP,        /*        complement the colours when highlihted. */
  134.   0x00000000,      /* MutualExclude, no mutualexclude. */
  135.   (APTR) &dice2,   /* ItemFill, pointer to the image. */
  136.   NULL,            /* SelectFill, nothing since we complement the col. */
  137.   0,               /* Command, no command-key sequence. */
  138.   NULL,            /* SubItem, no subitem list. */
  139.   MENUNULL,        /* NextSelect, no items selected. */
  140. };
  141.  
  142.  
  143.  
  144. /*************************************************************************/
  145. /*                             D I C E   3                               */
  146. /*************************************************************************/
  147.  
  148. /* Data for dice 3: */
  149. USHORT chip dice3_data[]=
  150. {
  151.   0x0000,  /* 0000 0000 0000 0000   0 : Black  */
  152.   0x7800,  /* 0111 1000 0000 0000   1 : Orange */
  153.   0x7800,  /* 0111 1000 0000 0000              */
  154.   0x03C0,  /* 0000 0011 1100 0000              */
  155.   0x03C0,  /* 0000 0011 1100 0000              */
  156.   0x001E,  /* 0000 0000 0001 1110              */
  157.   0x001E,  /* 0000 0000 0001 1110              */
  158.   0x0000   /* 0000 0000 0000 0000              */
  159. };
  160.  
  161. /* Image structure for dice 3: */
  162. struct Image dice3=
  163. {
  164.   0,          /* LeftEdge, 0 pixels out. */
  165.   0,          /* TopEdge, 0 pixels down. */
  166.   16,         /* Width, 16 pixels wide. */
  167.   8,          /* Height, 8 lines heigh. */
  168.   1,          /* Depth, one Bitplane. */
  169.   dice3_data, /* ImageData, pointer to the image data. */
  170.   0x1,        /* PlanePick, affect Bitplane zero. */
  171.   0x2,        /* PlaneOnOff, fill Bitplane one with 1's. */
  172.   NULL        /* NextImage, no Image structure connected to this one. */
  173. };
  174.  
  175. /* The MenuItem structure for dice 3: */
  176. struct MenuItem my_dice3_item=
  177. {
  178.   &my_dice2_item,  /* NextItem, pointer to the next item in the list. */
  179.   0,               /* LeftEdge, 0 pixels out. */
  180.   30,              /* TopEdge, 30 lines down. */
  181.   50,              /* Width, 50 pixels wide. */
  182.   8,               /* Height, 8 lines high. */
  183.   ITEMENABLED|     /* Flags, this item will be enabled. */
  184.                    /*        render this item with an Image. */
  185.                    /*        (ITEMTEXT is not set.) */
  186.                    /*        it is an action item. */
  187.                    /*        (CHECKIT is not set.) */
  188.   HIGHCOMP,        /*        complement the colours when highlihted. */
  189.   0x00000000,      /* MutualExclude, no mutualexclude. */
  190.   (APTR) &dice3,   /* ItemFill, pointer to the image. */
  191.   NULL,            /* SelectFill, nothing since we complement the col. */
  192.   0,               /* Command, no command-key sequence. */
  193.   NULL,            /* SubItem, no subitem list. */
  194.   MENUNULL,        /* NextSelect, no items selected. */
  195. };
  196.  
  197.  
  198.  
  199. /*************************************************************************/
  200. /*                             D I C E   4                               */
  201. /*************************************************************************/
  202.  
  203. /* Data for dice 4: */
  204. USHORT chip dice4_data[]=
  205. {
  206.   0x0000,  /* 0000 0000 0000 0000   0 : Black  */
  207.   0x781E,  /* 0111 1000 0001 1110   1 : Orange */
  208.   0x781E,  /* 0111 1000 0001 1110              */
  209.   0x0000,  /* 0000 0000 0000 0000              */
  210.   0x0000,  /* 0000 0000 0000 0000              */
  211.   0x781E,  /* 0111 1000 0001 1110              */
  212.   0x781E,  /* 0111 1000 0001 1110              */
  213.   0x0000   /* 0000 0000 0000 0000              */
  214. };
  215.  
  216. /* Image structure for dice 4: */
  217. struct Image dice4=
  218. {
  219.   0,          /* LeftEdge, 0 pixels out. */
  220.   0,          /* TopEdge, 0 pixels down. */
  221.   16,         /* Width, 16 pixels wide. */
  222.   8,          /* Height, 8 lines heigh. */
  223.   1,          /* Depth, one Bitplane. */
  224.   dice4_data, /* ImageData, pointer to the image data. */
  225.   0x1,        /* PlanePick, affect Bitplane zero. */
  226.   0x2,        /* PlaneOnOff, fill Bitplane one with 1's. */
  227.   NULL        /* NextImage, no Image structure connected to this one. */
  228. };
  229.  
  230. /* The MenuItem structure for dice 4: */
  231. struct MenuItem my_dice4_item=
  232. {
  233.   &my_dice3_item,  /* NextItem, pointer to the next item in the list. */
  234.   0,               /* LeftEdge, 0 pixels out. */
  235.   20,              /* TopEdge, 20 lines down. */
  236.   50,              /* Width, 50 pixels wide. */
  237.   8,               /* Height, 8 lines high. */
  238.   ITEMENABLED|     /* Flags, this item will be enabled. */
  239.                    /*        render this item with an Image. */
  240.                    /*        (ITEMTEXT is not set.) */
  241.                    /*        it is an action item. */
  242.                    /*        (CHECKIT is not set.) */
  243.   HIGHCOMP,        /*        complement the colours when highlihted. */
  244.   0x00000000,      /* MutualExclude, no mutualexclude. */
  245.   (APTR) &dice4,   /* ItemFill, pointer to the image. */
  246.   NULL,            /* SelectFill, nothing since we complement the col. */
  247.   0,               /* Command, no command-key sequence. */
  248.   NULL,            /* SubItem, no subitem list. */
  249.   MENUNULL,        /* NextSelect, no items selected. */
  250. };
  251.  
  252.  
  253.  
  254. /*************************************************************************/
  255. /*                             D I C E   5                               */
  256. /*************************************************************************/
  257.  
  258. /* Data for dice 5: */
  259. USHORT chip dice5_data[]=
  260. {
  261.   0x0000,  /* 0000 0000 0000 0000   0 : Black  */
  262.   0x781E,  /* 0111 1000 0001 1110   1 : Orange */
  263.   0x781E,  /* 0111 1000 0001 1110              */
  264.   0x03C0,  /* 0000 0011 1100 0000              */
  265.   0x03C0,  /* 0000 0011 1100 0000              */
  266.   0x781E,  /* 0111 1000 0001 1110              */
  267.   0x781E,  /* 0111 1000 0001 1110              */
  268.   0x0000   /* 0000 0000 0000 0000              */
  269. };
  270.  
  271. /* Image structure for dice 5: */
  272. struct Image dice5=
  273. {
  274.   0,          /* LeftEdge, 0 pixels out. */
  275.   0,          /* TopEdge, 0 pixels down. */
  276.   16,         /* Width, 16 pixels wide. */
  277.   8,          /* Height, 8 lines heigh. */
  278.   1,          /* Depth, one Bitplane. */
  279.   dice5_data, /* ImageData, pointer to the image data. */
  280.   0x1,        /* PlanePick, affect Bitplane zero. */
  281.   0x2,        /* PlaneOnOff, fill Bitplane one with 1's. */
  282.   NULL        /* NextImage, no Image structure connected to this one. */
  283. };
  284.  
  285. /* The MenuItem structure for dice 5: */
  286. struct MenuItem my_dice5_item=
  287. {
  288.   &my_dice4_item,  /* NextItem, pointer to the next item in the list. */
  289.   0,               /* LeftEdge, 0 pixels out. */
  290.   10,              /* TopEdge, 10 lines down. */
  291.   50,              /* Width, 50 pixels wide. */
  292.   8,               /* Height, 8 lines high. */
  293.   ITEMENABLED|     /* Flags, this item will be enabled. */
  294.                    /*        render this item with an Image. */
  295.                    /*        (ITEMTEXT is not set.) */
  296.                    /*        it is an action item. */
  297.                    /*        (CHECKIT is not set.) */
  298.   HIGHCOMP,        /*        complement the colours when highlihted. */
  299.   0x00000000,      /* MutualExclude, no mutualexclude. */
  300.   (APTR) &dice5,   /* ItemFill, pointer to the image. */
  301.   NULL,            /* SelectFill, nothing since we complement the col. */
  302.   0,               /* Command, no command-key sequence. */
  303.   NULL,            /* SubItem, no subitem list. */
  304.   MENUNULL,        /* NextSelect, no items selected. */
  305. };
  306.  
  307.  
  308.  
  309. /*************************************************************************/
  310. /*                             D I C E   6                               */
  311. /*************************************************************************/
  312.  
  313. /* Data for dice 6: */
  314. USHORT chip dice6_data[]=
  315. {
  316.   0x0000,  /* 0000 0000 0000 0000   0 : Black  */
  317.   0x7BDE,  /* 0111 1011 1101 1110   1 : Orange */
  318.   0x7BDE,  /* 0111 1011 1101 1110              */
  319.   0x0000,  /* 0000 0000 0000 0000              */
  320.   0x0000,  /* 0000 0000 0000 0000              */
  321.   0x7BDE,  /* 0111 1011 1101 1110              */
  322.   0x7BDE,  /* 0111 1011 1101 1110              */
  323.   0x0000   /* 0000 0000 0000 0000              */
  324. };
  325.  
  326. /* Image structure for dice 6: */
  327. struct Image dice6=
  328. {
  329.   0,          /* LeftEdge, 0 pixels out. */
  330.   0,          /* TopEdge, 0 pixels down. */
  331.   16,         /* Width, 16 pixels wide. */
  332.   8,          /* Height, 8 lines heigh. */
  333.   1,          /* Depth, one Bitplane. */
  334.   dice6_data, /* ImageData, pointer to the image data. */
  335.   0x1,        /* PlanePick, affect Bitplane zero. */
  336.   0x2,        /* PlaneOnOff, fill Bitplane one with 1's. */
  337.   NULL        /* NextImage, no Image structure connected to this one. */
  338. };
  339.  
  340. /* The MenuItem structure for dice 6: */
  341. struct MenuItem my_dice6_item=
  342. {
  343.   &my_dice5_item,  /* NextItem, pointer to the next item in the list. */
  344.   0,               /* LeftEdge, 0 pixels out. */
  345.   0,               /* TopEdge, 0 lines down. */
  346.   50,              /* Width, 50 pixels wide. */
  347.   8,               /* Height, 8 lines high. */
  348.   ITEMENABLED|     /* Flags, this item will be enabled. */
  349.                    /*        render this item with an Image. */
  350.                    /*        (ITEMTEXT is not set.) */
  351.                    /*        it is an action item. */
  352.                    /*        (CHECKIT is not set.) */
  353.   HIGHCOMP,        /*        complement the colours when highlihted. */
  354.   0x00000000,      /* MutualExclude, no mutualexclude. */
  355.   (APTR) &dice6,   /* ItemFill, pointer to the image. */
  356.   NULL,            /* SelectFill, nothing since we complement the col. */
  357.   0,               /* Command, no command-key sequence. */
  358.   NULL,            /* SubItem, no subitem list. */
  359.   MENUNULL,        /* NextSelect, no items selected. */
  360. };
  361.  
  362.  
  363.  
  364. /*************************************************************************/
  365. /*                              M E N U                                  */
  366. /*************************************************************************/
  367.  
  368. /* The Menu structure for the first (and only) menu: */
  369. struct Menu my_menu=
  370. {
  371.   NULL,          /* NextMenu, no more menu structures. */
  372.   0,             /* LeftEdge, left corner. */
  373.   0,             /* TopEdge, for the moment ignored by Intuition. */
  374.   50,            /* Width, 50 pixels wide. */
  375.   0,             /* Height, for the moment ignored by Intuition. */
  376.   MENUENABLED,   /* Flags, this menu will be enabled. */
  377.   "Dice",        /* MenuName, the string. */
  378.   &my_dice6_item /* FirstItem, pointer to the first item in the list. */
  379. };
  380.  
  381.  
  382.  
  383. /* Declare a pointer to a Window structure: */ 
  384. struct Window *my_window;
  385.  
  386. /* Declare and initialize your NewWindow structure: */
  387. struct NewWindow my_new_window=
  388. {
  389.   50,            /* LeftEdge    x position of the window. */
  390.   25,            /* TopEdge     y positio of the window. */
  391.   200,           /* Width       200 pixels wide. */
  392.   100,           /* Height      100 lines high. */
  393.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  394.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  395.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  396.                  /*             user has selected the Close window gad. */
  397.   MENUPICK,
  398.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  399.   WINDOWCLOSE|   /*             Close Gadget. */
  400.   WINDOWDRAG|    /*             Drag gadget. */
  401.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  402.   WINDOWSIZING|  /*             Sizing Gadget. */
  403.   ACTIVATE,      /*             The window should be Active when opened. */
  404.   NULL,          /* FirstGadget No Custom gadgets. */
  405.   NULL,          /* CheckMark   Use Intuition's default checkmark. */
  406.   "GAME",        /* Title       Title of the window. */
  407.   NULL,          /* Screen      Connected to the Workbench Screen. */
  408.   NULL,          /* BitMap      No Custom BitMap. */
  409.   80,            /* MinWidth    We will not allow the window to become */
  410.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  411.   300,           /* MaxWidth    than 300 x 200. */
  412.   200,           /* MaxHeight */
  413.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  414. };
  415.  
  416.  
  417.  
  418. main()
  419. {
  420.   /* Boolean variable used for the while loop: */
  421.   BOOL close_me;
  422.  
  423.   /* Declare a variable in which we will store the IDCMP flag: */
  424.   ULONG class;
  425.   
  426.   /* If we recieve a MENUPICK event, the Code field of the message */
  427.   /* structure will contain the menu number of the first selected item. */
  428.   /* Declare a variable to store the Code value in, and an extra menu */
  429.   /* number variable: */
  430.   USHORT code, menu_number;
  431.   
  432.   /* Declare a MenuItem pointer: */
  433.   struct MenuItem *item;
  434.   
  435.   /* Declare a pointer to an IntuiMessage structure: */
  436.   struct IntuiMessage *my_message;
  437.  
  438.  
  439.  
  440.   /* Before we can use Intuition we need to open the Intuition Library: */
  441.   IntuitionBase = (struct IntuitionBase *)
  442.     OpenLibrary( "intuition.library", 0 );
  443.   
  444.   if( IntuitionBase == NULL )
  445.     exit(); /* Could NOT open the Intuition Library! */
  446.  
  447.  
  448.  
  449.   /* We will now try to open the window: */
  450.   my_window = (struct Window *) OpenWindow( &my_new_window );
  451.   
  452.   /* Have we opened the window succesfully? */
  453.   if(my_window == NULL)
  454.   {
  455.     /* Could NOT open the Window! */
  456.     
  457.     /* Close the Intuition Library since we have opened it: */
  458.     CloseLibrary( IntuitionBase );
  459.  
  460.     exit();  
  461.   }
  462.  
  463.  
  464.  
  465.   /* We have opened the window, and everything seems to be OK. */
  466.  
  467.  
  468.  
  469.   SetMenuStrip( my_window, &my_menu );
  470.   printf("Menustrip connected to window!\n");
  471.  
  472.  
  473.   close_me = FALSE;
  474.  
  475.   /* Stay in the while loop until the user has selected the Close window */
  476.   /* gadget: */
  477.   while( close_me == FALSE )
  478.   {
  479.     /* Wait until we have recieved a message: */
  480.     Wait( 1 << my_window->UserPort->mp_SigBit );
  481.  
  482.     /* As long as we collect messages sucessfully we stay in the loop: */
  483.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  484.     {
  485.       /* After we have collected the message we can read it, and save any */
  486.       /* important values which we maybe want to check later: */
  487.       class = my_message->Class;
  488.       code = my_message->Code;
  489.  
  490.  
  491.       /* After we have read it we reply as fast as possible: */
  492.       /* REMEMBER! Do never try to read a message after you have replied! */
  493.       /* Some other process has maybe changed it. */
  494.       ReplyMsg( my_message );
  495.  
  496.       /* Check which IDCMP flag was sent: */
  497.       if( class == CLOSEWINDOW )
  498.         close_me=TRUE; /* The user selected the Close window gadget! */  
  499.  
  500.       if(class == MENUPICK)
  501.       {
  502.         printf("\nMenu pick!\n");
  503.         menu_number = code;
  504.         
  505.         while( menu_number != MENUNULL )
  506.         {
  507.           /* Get the address of the item: */
  508.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  509.  
  510.  
  511.           /* Print out the menu number plus etc: */
  512.           printf("menu_number= %d\n", menu_number );
  513.           printf("MENUNUM = %d\n", MENUNUM(menu_number) );
  514.           printf("ITEMNUM = %d\n", ITEMNUM(menu_number) );
  515.           printf("SUBNUM  = %d\n", SUBNUM(menu_number) );
  516.  
  517.  
  518.           /* Get the following item's menu number: */
  519.           menu_number = item->NextSelect;
  520.         }
  521.       }
  522.     }
  523.   }
  524.  
  525.  
  526.  
  527.   printf("Menustrip removed from window!\n");
  528.   ClearMenuStrip( my_window );
  529.  
  530.  
  531.  
  532.   /* Close the window: */
  533.   CloseWindow( my_window );
  534.  
  535.  
  536.  
  537.   /* Close the Intuition Library since we have opened it: */
  538.   CloseLibrary( IntuitionBase );
  539.   
  540.   /* THE END */
  541. }
  542.